#include "Attack.h"
#include "Player.h"
#include "Monster.h"
#include <ctime>    
#include <cstdlib>  

// yeah, i know still need to implement evade calculation... to do later.

void Attack::attack(Player& p, Monster& m, int a)
{
	int attackTypeBaseDamage = 0;      // assume default attack type base damage is 0.   
	
	AttackType anAttackType;
	if( p.getAttackType(a, anAttackType) )  // if caller chooses a valid index a
	{
		int attackActionCost   = anAttackType.getActionPointsCost();
		int playerActionPoints = p.getPlayerStats().getActionPoints();
		  
		if( playerActionPoints >= attackActionCost )  // if player has enough AP to peform attack
		{
			int playerActionPointsAfterAttack = playerActionPoints - attackActionCost;
			p.getPlayerStats().setActionPoints( playerActionPointsAfterAttack );
			
			attackTypeBaseDamage = anAttackType.getBaseDamage();  // apply attack damage, otherwise defaults to 0.
		}			
	}
	
	const vector<EquipmentType*>& playerEquipment = p.getEquipment();
	int weaponDamage = 0;
	if( playerEquipment[5] != 0 )  // check if player weapon equipped
		weaponDamage = playerEquipment[5]->getDamage();

	const vector<EquipmentType*>& monsterEquipment = m.getEquipment();
	int monsterArmor = 0;
	for( int i = 0; i < 4; i++ )   // add up monster armor pieces in slots
		if( monsterEquipment[i] != 0 )
			monsterArmor += monsterEquipment[i]->getArmor();
	
	int playerBonusStat = 0;
	if( p.getCharClass() == WARRIOR )
		playerBonusStat = p.getPlayerStats().getStrength();
	else if( p.getCharClass() == WIZARD )
		playerBonusStat = p.getPlayerStats().getIntelligence();
	else 
		playerBonusStat = p.getPlayerStats().getConcentration();
	
	int weightedDamage = weaponDamage + attackTypeBaseDamage + (playerBonusStat / 20);   // +5% bonus damage of player's special stat
	int mitgatedDamage = weightedDamage - monsterArmor;                  		 
	
	int monsterHitPointAfterAttack = m.getMonsterStats().getHitPoints() - mitgatedDamage;
	m.getMonsterStats().setHitPoints( monsterHitPointAfterAttack );
	
	if( monsterHitPointAfterAttack <= 0 )
		m.setDead( true );
}

void Attack::attack(Monster& m, Player& p)
{
	int attackTypeBaseDamage = 0;      // assume default attack type base damage is 0.   
	
	srand( time(0) );  int r = rand() % m.getNumAttacks();  // random attack type r
	
	AttackType anAttackType;
	if( m.getAttackType(r, anAttackType) )  // will probably be true all the time, just in case vector.size() = 0;
	{
		int attackActionCost    = anAttackType.getActionPointsCost();
		int monsterActionPoints = m.getMonsterStats().getActionPoints();
		  
		if( monsterActionPoints >= attackActionCost )  // if monster has enough AP to peform attack
		{
			int monsterActionPointsAfterAttack = monsterActionPoints - attackActionCost;
			m.getMonsterStats().setActionPoints( monsterActionPointsAfterAttack );
			
			attackTypeBaseDamage = anAttackType.getBaseDamage();  // apply attack damage, otherwise defaults to 0.
		}			
	}
	
	const vector<EquipmentType*>& monsterEquipment = p.getEquipment();
	int weaponDamage = 0;
	if( monsterEquipment[5] != 0 )  // check if monster weapon equipped
		weaponDamage = monsterEquipment[5]->getDamage();

	const vector<EquipmentType*>& playerEquipment = m.getEquipment();
	int playerArmor = 0;
	for( int i = 0; i < 4; i++ )   // add up monster armor pieces in slots
		if( playerEquipment[i] != 0 )
			playerArmor += playerEquipment[i]->getArmor();
		
	int weightedDamage = weaponDamage + attackTypeBaseDamage;
	int mitgatedDamage = weightedDamage - playerArmor;                  		 
	
	int playerHitPointAfterAttack = p.getPlayerStats().getHitPoints() - mitgatedDamage;
	p.getPlayerStats().setHitPoints( playerHitPointAfterAttack );
	
	if( playerHitPointAfterAttack <= 0 )
		p.setDead( true );
}